﻿namespace DslEditorPowerToy.Controls.Languages.ToolWindows
{
    using DslEditorPowerToy.Controls;
    using DslEditorPowerToy.Controls.Languages;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell.Interop;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Windows.Forms;

    [CLSCompliant(false)]
    public abstract class EditorHostContainerBase : ControlHostBase, IEditorHostContainer
    {
        private EditorHostControl _control;

        // jk, 20120923, --> Guid editorId
        public EditorHostContainerBase(IServiceProvider serviceProvider, System.Type dslLanguageDocViewType, Assembly editorDeclaringAssembly, Guid editorId) : base(serviceProvider, dslLanguageDocViewType)
        {
            this._control = new EditorHostControl(editorDeclaringAssembly, editorId);
            this._control.EditorChanged += new EventHandler(this._control_EditorChanged);
        }

        private void _control_EditorChanged(object sender, EventArgs e)
        {
            this.OnEditorChanged();
        }

        public void ActivateEditor(Guid editorId)
        {
            this._control.ActivateEditor(editorId);
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing && (this._control != null))
                {
                    this._control.EditorChanged -= new EventHandler(this._control_EditorChanged);
                    this._control.Dispose();
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        private void ModifyWindowTitle()
        {
            if (((this.CurrentEditor != null) && !string.IsNullOrEmpty(this.CurrentEditor.Caption)) && (this.Editors.Count > 1))
            {
                string title = string.Format("{0} - [{1}]", this.WindowTitle, this.CurrentEditor.Caption);
                this.SetWindowTitle(title);
            }
        }

        protected virtual void OnEditorChanged()
        {
            this.ModifyWindowTitle();
        }

        private void SetWindowTitle(string title)
        {
            IVsWindowFrame frame = base.Frame;
            if (frame != null)
            {
                ErrorHandler.ThrowOnFailure(frame.SetProperty(-3004, title));
            }
        }

        protected override System.Windows.Forms.Control Control
        {
            get
            {
                return this._control;
            }
        }

        public IHostedEditorDefinition CurrentEditor
        {
            get
            {
                return this._control.CurrentEditor;
            }
        }

        public List<IHostedEditorDefinition> Editors
        {
            get
            {
                return this._control.GetEditors();
            }
        }

        public NavigationOrientationStyle NavigationStyle
        {
            get
            {
                return this._control.NavigationStyle;
            }
            set
            {
                this._control.NavigationStyle = value;
            }
        }
    }
}

